package cn.btks.resultintegration.achievements.repository;

import cn.btks.resultintegration.achievements.pojo.AchievementsFile;
import cn.btks.resultintegration.achievements.pojo.AchievementsFileTemp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Author lijun
 * @Date 2020/12/17 12:41
 */
@Repository
public interface AchievementsFileRepository extends JpaRepository<AchievementsFile,Long> {

    /**
     * 根据文件名查询所有文件
     * @param fileName
     * @return
     */
    @Query(value = "select * from achievements_file where file_name = ?1 and is_history = 0",nativeQuery = true)
    List<AchievementsFile> findAllByFileName(String fileName);

    /**
     * 根据成果ID及是否历史数据查询成果文件
     * @param achievementsId
     * @param isHistory
     * @return
     */@Query(value = "select * from achievements_file where achievements_id = ?1 and is_history = ?2",nativeQuery = true)
    List<AchievementsFile> findByAchievementsIdAndIsHistory(Long achievementsId,Integer isHistory);

    /**
     * 根据成果ID及成果类型查询成果文件
     * @param achievementsId
     * @param typeId
     * @return
     */
    @Query(value = "select * from achievements_file where achievements_id = ?1 and achievements_type_id = ?2",nativeQuery = true)
    List<AchievementsFile> findByAchievementsIdAndTypeId(Long achievementsId,Long typeId);

    /**
     * 根据成果ID及类型更新成果数据为历史数据
     * @param achievementsId
     * @param typeId
     * @param isHistory
     * @return
     */
    @Modifying
    @Query(value = "update achievements_file set is_history = ?3 where achievements_id = ?1 and achievements_type_id = ?2",nativeQuery = true)
    int updateFileIsHistory(Long achievementsId,Long typeId,Integer isHistory);

    /**
     * 根据类型更新成果数据为历史数据
     * @param vectorTypeId
     * @param textTypeId
     * @param isHistory
     * @return
     */
    @Modifying
    @Query(value = "update achievements_file set is_history = ?3 where achievements_type_id = ?1 or achievements_type_id = ?2",nativeQuery = true)
    int updateFileIsHistoryByTypeId(Long vectorTypeId,Long textTypeId,Integer isHistory);

    /**
     * 根据成果ID更新成果数据为历史数据
     * @param achievementsId
     * @param isHistory
     * @return
     */
    @Modifying
    @Query(value = "update achievements_file set is_history = ?2 where achievements_id = ?1",nativeQuery = true)
    int updateFileIsHistory(Long achievementsId,Integer isHistory);

    /**
     * 根据成果ID及类型删除成果文件
     * @param achievementsId
     * @param typeId
     * @return
     */
    @Modifying
    @Query(value = "delete from achievements_file where achievements_id = ?1 and achievements_type_id = ?2",nativeQuery = true)
    int deleteByAchievementsIdAndTypeId(Long achievementsId,Long typeId);

    /**
     * 根据成果ID及类型删除文本、矢量成果文件
     * @param achievementsId
     * @param textTypeId
     * @param vectorTypeId
     * @return
     */
    @Modifying
    @Query(value = "delete from achievements_file where achievements_id = ?1 and (achievements_type_id = ?2 or achievements_type_id = ?3)",nativeQuery = true)
    int deleteByAchievementsIdAndTextTypeIdOrVectorTypeId(Long achievementsId,Long textTypeId,Long vectorTypeId);
}
